
import {
  cooperationBehavior
} from "../../behaviors/cooperation";
import api from "../../common/api";
import constant from '../../common/constant';
Component({
  options: {
    styleIsolation: "apply-shared",
    addGlobalClass: true,
  },
  properties: {
    house: {
      type: Object,
      value: {}
    },
    PageFrom:{
      type:String,
      value: ''
    }
  },
  behaviors: [cooperationBehavior],
  data: {
    headPortraitList:[],
    upOrDown:'', //查看访客列表时判断是下架中还是已上架
    isShowFocusFTQR: false, //查看访客列表时  是否出现房态弹窗
  },

  lifetimes: {
    attached: function () {
      getApp().globalData.loginPromise.then(() => {
        let imgList = this.data.house.vistorList
        if(imgList){
          if( 5<imgList.length){
            this.setData({
              headPortraitList:imgList.slice(0,5)
            })
          } else {
            this.setData({
              headPortraitList:imgList
            })
          }
        }
      })
    },
    detached: function () {},
  },
  methods: {
    handleCopyPhoneNum() {
      wx.setClipboardData({
        data: this.data.house.wxMobile,
        success: () => {
          wx.showToast({
            title: '手机号复制成功',
          })
        }
      })
    },
    onImgErr(e) {
      let t = this
      t.setData({
        'house.houseImg': '/images/defaultImgSmall.jpg'
      })
    },
    //列表跳转且在url中传值
    onJumpHouseDetail() {
      let t = this
      let {
        house,
      } = t.data

      if (house.sceneType === 'publish') { //我的房源
        wx.gio('track', constant.openHouseDetail, {
          [constant.FromPage]: '我的房源'
        })
        wx.navigateTo({
          url: `/pages/house/RentalHouseDetail/coopertionHouseInfo/coopertionHouseInfo?cooperationId=${house.houseID}`,
        });
      }
      if (house.sceneType === 'advisory') { //我咨询过的
        wx.gio('track', constant.openHouseDetail, {
          [constant.FromPage]: '我咨询过的'
        })
        if (house.houseStatus === 2 || house.houseStatus === 1) {
          wx.showToast({
            title: '该套房源，合作方已下架，暂时无法查看',
            icon: 'none',
          })
        }
        if (house.houseStatus === 0) {
          if (house.houseIsCopy) {
            wx.navigateTo({
              url: `/pages/house/RentalHouseDetail/RentalHouseDetail?houdelid=${house.houseCopyID}&from=2&type=${house.houseType}&cooperation=${house.houseID}`,
            });
          } else {
            wx.navigateTo({
              url: `/pages/house/RentalHouseDetail/teamworkHouseDetail/teamworkHouseDetail?cooperationId=${house.houseID}`,
            });
          }
        }
      }
    },

    //查看访客列表
    visitorsRecord(){
      let {downHouseDay,cooperationStatus} = this.data.house
      //(1)，先判断是上架中还是已下架
        if(cooperationStatus!==0){ //下架中
          this.setData({
            isShowFocusFTQR:true,
            upOrDown:true
          })
        } else {
          //(2)在判断上架中的是不时三天内即将下架
          if(downHouseDay){
            this.setData({
              isShowFocusFTQR:true
            })
          } else {
            this.gotoBrowse()
          }
        }
    },


    // 咨询记录
    onJumpAdvisoryHistory() {
      let t = this
      let {
        house
      } = t.data
      wx.navigateTo({
        url: `/pages/cooperation/consultHistory/consultHistory?id=${house.houseID}`,
      });
    },

    onInvite() {
      let t = this
      let {
        house
      } = t.data
      wx.navigateTo({
        url: `/pages/im/imMessagelist/imMessagelist?unionid=${house.brokerUnionid}&cooperationId=${house.houseID}`,
      })
    },

    // 下架合作
    onUnShelve() {
      let t = this
      let {
        house
      } = t.data
      t.triggerEvent('unShelve', house.houseID)
    },

    onChatHistory() {
      let t = this
      let {
        house
      } = t.data
      wx.navigateTo({
        url: `/pages/im/imMessagelist/imMessagelist?unionid=${house.brokerUnionid}&pageFrom=${t.data.PageFrom}`,
      })
    },

    // 重新上架
    onBack() {
      let t = this
      let {
        house
      } = t.data
      t.triggerEvent('onBackX', house.houseOldID)
    },

    // 修改房源
    gotoEditHouseInfo(e) {
      let cooperationId = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '/pages/cooperation/editHouseInfo/editHouseInfo?cooperationId=' + cooperationId,
      })
    },
    // 单纯的去访客列表页
    gotoBrowse(){
      let houseID = this.data.house.houseID
      if(houseID){
        wx.navigateTo({
          url: `/pages/visitorsRecord/visitorsRecord?cooperationId=${houseID}`,
        })
      } else {
        wx.showToast({
          title: '房源信息不存在',
          icon: 'none',
          duration: 500
        })
      }
    },
  }
})